package com.feng.seata.web;

import com.feng.common.model.Result;
import com.feng.seata.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/orders")
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @GlobalTransactional(rollbackFor = Exception.class)
    @PostMapping("/seata/test")
    public Result<Void> seataDemo(Integer buyNum, Boolean hasError) {
        orderService.seataDemo(buyNum, hasError);
        return Result.ok();
    }

    @Transactional
    @PostMapping("/seata/withoutTx")
    public Result<Void> seataWithoutGlobalTx(Integer buyNum, Boolean hasError) {
        orderService.seataDemo(buyNum, hasError);
        return Result.ok();
    }

}
